from django.core.management.base import BaseCommand
from django.db import transaction
from events.service.storage_simulator_service import StorageSimulatorService
from django.utils import timezone

class Command(BaseCommand):
    help = '执行库存更新服务'

    def handle(self, *args, **options):
        try:
            with transaction.atomic():
                service = StorageSimulatorService(transaction.get_connection())
                
                # 顺序执行两个函数
                self.stdout.write(self.style.SUCCESS('开始执行update_today_replenishment...'))
                service.update_today_replenishment()
                self.stdout.write(self.style.SUCCESS('update_today_replenishment执行完成'))
                
                self.stdout.write(self.style.SUCCESS('开始执行process_seven_days_ago_replenishment...'))
                service.process_seven_days_ago_replenishment()
                self.stdout.write(self.style.SUCCESS('process_seven_days_ago_replenishment执行完成'))
                
        except Exception as e:
            self.stdout.write(self.style.ERROR(f'执行失败: {str(e)}'))